<?php
/* @var $this PositionController */
/* @var $dataProvider CActiveDataProvider */

$this->breadcrumbs=array(
	'Positions',
);

$this->menu=array(
	array('label'=>'Create Position', 'url'=>array('create')),
	array('label'=>'Manage Position', 'url'=>array('admin')),
);
?>

<h1>Positions</h1>

<?php /*$this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
));*/ ?>

<SCRIPT type="text/javascript">
getLoader();
getZTree();	

		var setting = {
			view: {
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom,
				selectedMulti: false
			},
			edit: {
				enable: true,
				// editNameSelectAll: true,
				// 实现拖拽
		    // enable: true,
			  //showRemoveBtn: false,
			  //showRenameBtn: false,
			  // editNameSelectAll: true
			},
			data: {
				simpleData: {
					enable: true,
					idKey: "id",
				  pIdKey: "parentid",
				  rootPid:""
				}
			},
			callback: {
				beforeDrag: beforeDrag,
			  onDrag: onDrag,
			  beforeDrop: beforeDrop,
			  onDrop: onDrop,
				beforeEditName: beforeEditName,
				beforeRemove: beforeRemove,
				beforeRename: beforeRename,
				onRemove: onRemove,
				onRename: onRename
			}
		};

		/*var zNodes =[
			{ id:1, pId:0, name:"父节点 1", open:true},
			{ id:11, pId:1, name:"叶子节点 1-1"},
			{ id:12, pId:1, name:"叶子节点 1-2"},
			{ id:13, pId:1, name:"叶子节点 1-3"},
			{ id:2, pId:0, name:"父节点 2", open:true},
			{ id:21, pId:2, name:"叶子节点 2-1"},
			{ id:22, pId:2, name:"叶子节点 2-2"},
			{ id:23, pId:2, name:"叶子节点 2-3"},
			{ id:3, pId:0, name:"父节点 3", open:true},
			{ id:31, pId:3, name:"叶子节点 3-1"},
			{ id:32, pId:3, name:"叶子节点 3-2"},
			{ id:33, pId:3, name:"叶子节点 3-3"}
		];*/
		var log, className = "dark";
		function beforeDrag(treeId, treeNodes) {
			for (var i=0,l=treeNodes.length; i<l; i++) {
				if (treeNodes[i].drag === false) {
					return false;
				}
			}
			return true;
		}
		function onDrag(){
	    return true;
	  }
	
	  function beforeDrop(treeId, treeNodes, targetNode, moveType){
	    console.log('beforeDrop:');
			console.log('treeId:');
			console.log(treeId);
			console.log('treeNodes:');
			console.log(treeNodes);
			console.log('targetNode:');
			console.log(targetNode);
			console.log('moveType:');
			console.log(moveType);
			return true;
	  }
	
	  function onDrop(){
	    return true;
	  }
		function beforeEditName(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			// showLog("[ "+getTime()+" beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
			var zTree = $.fn.zTree.getZTreeObj("positionTree");
			zTree.selectNode(treeNode);
			/*var text = $.ajax({
			  url:'./index.php?r=position/update',
				data:{'id':treeNode.id,'parentid':treeNode.parentid,'name':treeNode.name}
			}).responeText;
			// console.log(text);*/
			return true; // confirm("进入节点 --"+ treeNode.id +","+ treeNode.parentid +","+ treeNode.name + " 的编辑状态吗？");
		}
		function beforeRemove(treeId, treeNode) {
			className = (className === "dark" ? "":"dark");
			showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
			var zTree = $.fn.zTree.getZTreeObj("positionTree");
			zTree.selectNode(treeNode);
			if (confirm("确认删除 " + treeNode.name + " 吗？")){
			var text = $.ajax({
			  url:'./index.php?r=position/delete',
				data:{'id':treeNode.id,'Position[id]':treeNode.id,'Position[parentid]':treeNode.parentid},
				type:'POST',
				async:false,
				dataType:'json',
				/*success:function(data){
				  //console.log('data:');
				  //console.log(data);
				},
				error:function(){
				}*/
			}).responseText;
			  var data = eval('('+text+')');
				$.messagetip({message:data.info, fadeintime:2000});
				return data.status;
			}
			// return true;//confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
		}
		function onRemove(e, treeId, treeNode) {
			showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		}
		function beforeRename(treeId, treeNode, newName) {
			className = (className === "dark" ? "":"dark");
			showLog("[ "+getTime()+" beforeRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
			if (newName.length == 0) {
				alert("名称不能为空.");
				var zTree = $.fn.zTree.getZTreeObj("positionTree");
				setTimeout(function(){zTree.editName(treeNode)}, 10);
				return false;
			}
			// alert(newName);
			// 从数据库中更改职位名称
			//console.log(treeNode);
			var text = $.ajax({
			  url:'./index.php?r=position/update',
				data:{'id':treeNode.id,'Position[id]':treeNode.id,'Position[parentid]':treeNode.parentid,'Position[name]':newName},
				type:'POST',
				async:false,
				dataType:'json',
				/*success:function(data){
				  //console.log('data:');
				  //console.log(data);
				},
				error:function(){
				}*/
			}).responseText;
			  var data = eval('('+text+')');
				$.messagetip({message:data.info, fadeintime:2000});
			return true;
		}
		function onRename(e, treeId, treeNode) {
			showLog("[ "+getTime()+" onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		}
		function showLog(str) {
			if (!log) log = $("#log");
			log.append("<li class='"+className+"'>"+str+"</li>");
			if(log.children("li").length > 8) {
				log.get(0).removeChild(log.children("li")[0]);
			}
		}
		function getTime() {
			var now= new Date(),
			h=now.getHours(),
			m=now.getMinutes(),
			s=now.getSeconds(),
			ms=now.getMilliseconds();
			return (h+":"+m+":"+s+ " " +ms);
		}

		var newCount = 1;
		function addHoverDom(treeId, treeNode) {
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0) return;
			var addStr = "<span class='button add' id='addBtn_" + treeNode.id
				+ "' title='add node' onfocus='this.blur();'></span>";
			sObj.after(addStr);
			var btn = $("#addBtn_"+treeNode.id);
			if (btn) btn.bind("click", function(){
				var zTree = $.fn.zTree.getZTreeObj("positionTree");
				zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
				return false;
			});
		};
		function removeHoverDom(treeId, treeNode) {
			$("#addBtn_"+treeNode.id).unbind().remove();
		};
		function selectAll() {
			var zTree = $.fn.zTree.getZTreeObj("positionTree");
			zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
		}
		
		$(document).ready(function(){
			// $.fn.zTree.init($("#positionTree"), setting, zNodes);
			// $("#selectAll").bind("click", selectAll);
			var zNodes = <?php echo $jsonTree; ?>;
	    var zTree = $.fn.zTree.init($("#positionTree"), setting, zNodes);
	    // var zTree = $.fn.zTree.getZTreeObj("positionTree");
      zTree.expandAll(true);
		});
	</SCRIPT>
<style type="text/css">
.ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
</style>
<h1>职位架构编辑（增删改）</h1>
<div class="">
	<ul id="positionTree" class="ztree"></ul>
</div> 